package com.ainiyar.springweb.util;

import com.ainiyar.springweb.common.exception.CustomerAuthenticationException;
import jakarta.servlet.http.HttpServletRequest;

/**************************************
 * File Name : TokenUtil
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/8  13:04
 * Desc :处理请求中的token
 ***************************************/
public class TokenUtil {
    public static String handelToken(HttpServletRequest request){
        //获取前端提交过来的token信息
        //先从headers中获取token信息
        String token = request.getHeader("Authorization");
        //如果请求头部中没有携带token，则从请求参数中获取token
        if (ToolsUtil.isNullOrEmpty(token)) {
            token = request.getParameter("Authorization");
        }
        if (token!=null && !token.startsWith("Bear ")){
            throw new CustomerAuthenticationException("token无效");
        }
        assert token != null;
        token=token.substring(5);
        return token;
    }
}

